#include "mainwindow.hh"
#include "settingsdialog.hh"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    createMenus();
    m_editorWidget = new EditorWidget();
    m_statusBar = new QStatusBar(this);

    connect(m_editorWidget, SIGNAL(cursorPositionChanged()), this, SLOT(updateStatusBar()));
    connect(m_editorWidget->document(), SIGNAL(contentsChanged()), this, SLOT(documentWasModified()));
    updateStatusBar();

    setCentralWidget(m_editorWidget);
    setStatusBar(m_statusBar);


    setCurrentFile("");
}

MainWindow::~MainWindow()
{

}

void MainWindow::showSettingsDialog(void)
{
    SettingsDialog* dialog = new SettingsDialog(this);
    dialog->show();
}

void MainWindow::documentWasModified(void)
{
    setWindowModified(m_editorWidget->document()->isModified());
}

void MainWindow::openFile(void)
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open"), "", "SASM Files (*.sasm)");
    if(fileName.isEmpty())
        return;
    QFile f(fileName);
    if(!f.open(QIODevice::ReadOnly))
        return;
    m_editorWidget->setText(f.readAll());
    f.close();
    m_currentFile = QFileInfo(fileName).fileName();
    setWindowModified(false);

    setCurrentFile(fileName);
}
void MainWindow::save(void)
{
    if(m_currentFile.isEmpty())
    {
        saveAs();
    }
    else
    {
        saveFile(m_currentFile);
    }
}

void MainWindow::saveAs()
{
    QString fileName = QFileDialog::getSaveFileName(this);
    if (fileName.isEmpty())
        return;
    saveFile(fileName);
}

void MainWindow::saveFile(const QString& fileName)
{
    QFile file(fileName);
    if (!file.open(QFile::WriteOnly | QFile::Text)) {
             QMessageBox::warning(this, tr("Application"),
                                  tr("Cannot write file %1:\n%2.")
                                  .arg(fileName)
                                  .arg(file.errorString()));
             return;
    }
    QTextStream out(&file);
#ifndef QT_NO_CURSOR
    QApplication::setOverrideCursor(Qt::WaitCursor);
#endif
     out << m_editorWidget->toPlainText();
#ifndef QT_NO_CURSOR
     QApplication::restoreOverrideCursor();
#endif
    setCurrentFile(fileName);
}

void MainWindow::setCurrentFile(const QString& fileName)
{
    m_currentFile = fileName;
    m_editorWidget->document()->setModified(false);
    setWindowModified(false);

    QString shownName = m_currentFile;
    if (m_currentFile.isEmpty())
        shownName = "Unnamed.sasm";
    setWindowFilePath(shownName);
}

void MainWindow::updateStatusBar(void)
{
    m_statusBar->showMessage(QString("Line: %1 Column: %2").arg(m_editorWidget->line())
                                                             .arg(m_editorWidget->column())
                             );
}

void MainWindow::createMenus()
{
    m_fileMenu = menuBar()->addMenu(tr("File"));
    m_fileMenu->addAction(tr("&New"));
    m_fileMenu->addAction(tr("&Open"),this, SLOT(openFile()));
    m_fileMenu->addAction(tr("&Save"), this, SLOT(save()))->setShortcut(Qt::CTRL + Qt::Key_S);
    m_fileMenu->addAction(tr("Save &As..."), this, SLOT(saveAs()));
    m_fileMenu->addSeparator();
    m_fileMenu->addAction(tr("Exit"));


    m_editMenu = menuBar()->addMenu(tr("Edit"));
    m_editMenu->addSeparator();
    m_editMenu->addAction(tr("Settings"), this, SLOT(showSettingsDialog()));
}
